﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace PongGame
{
    public enum GameMode { Menu, Game, GameOver }

    public class MenuManager
    {
        int currentMenuItem = 0;
        float leftPaddlePosition = 0.5f, rightPaddlePosition = 0.5f;
        
        bool remUpPressed = false,
            remDownPressed = false,
            remSpaceOrStartPressed = false,
            remEscOrBackPressed = false;

        public bool GamePadUp { get; set; }
        public bool GamePadDown { get; set; }
        public bool GamePad2Up { get; set; }
        public bool GamePad2Down { get; set; }

        Game game;
        SpriteManager spriteMgr;
        GamePadState gamePadState, gamePad2State;
        KeyboardState keyboardState;
        SoundBank soundBank;
        public GameMode gameMode = GameMode.Menu;//Preset to menu state mode
        GameManager GameMgr;

        public GameManager GameManager
        { get { return GameMgr; } }

        public KeyboardState KeyBoard
        { get { return keyboardState; } }

        public GamePadState GamePad
        { get { return gamePadState; } }

        public GamePadState GamePad2
        { get { return gamePad2State; } }

        public MenuManager(SpriteManager SpriteMgr, Game game, SoundBank sound)
        {
            spriteMgr = SpriteMgr;
            this.game = game;
            soundBank = sound;
        }

        public void Update()
        {
            GetCurrentPadAndKeyboardState();

            if (gameMode == GameMode.Menu)
            {
                SelectMenuItem();

                RecordKeyLastState();

                spriteMgr.PrepareSpriteMenu(currentMenuItem);
            }
            else
            {
                spriteMgr.ShowLives();

                spriteMgr.RenderBall();// Ball in center

                spriteMgr.RenderPaddles();// Render both paddles

                // If game is over, show winner
                if (gameMode == GameMode.GameOver)
                {
                   spriteMgr.ShowWinner();

                   //Press Space or Enter or Esc or Back returns to the main menu.
                   if ((gamePadState.Buttons.A == ButtonState.Pressed ||
                       keyboardState.IsKeyDown(Keys.Space) ||
                       keyboardState.IsKeyDown(Keys.Enter)) &&
                       remSpaceOrStartPressed == false)
                      gameMode = GameMode.Menu;
                }

                // Back and Escape always quits to the main menu
                if ((gamePadState.Buttons.Back == ButtonState.Pressed || 
                   keyboardState.IsKeyDown(Keys.Escape)) && remEscOrBackPressed == false)
                {
                    gameMode = GameMode.Menu;
                    GameMgr.StopBall();
                }
                GameMgr.MovePaddles();
                GameMgr.MoveBall();
            }
        }

        void RecordKeyLastState()
        {
            remUpPressed =
                gamePadState.DPad.Up == ButtonState.Pressed ||
                gamePadState.ThumbSticks.Left.Y > 0.5f ||
                keyboardState.IsKeyDown(Keys.Up);

            remDownPressed =
                gamePadState.DPad.Down == ButtonState.Pressed ||
                gamePadState.ThumbSticks.Left.Y < -0.5f ||
                keyboardState.IsKeyDown(Keys.Down);
            
            remSpaceOrStartPressed =
                gamePadState.Buttons.Start == ButtonState.Pressed ||
                gamePadState.Buttons.A == ButtonState.Pressed ||
                keyboardState.IsKeyDown(Keys.LeftControl) ||
                keyboardState.IsKeyDown(Keys.RightControl) ||
                keyboardState.IsKeyDown(Keys.Space) ||
                keyboardState.IsKeyDown(Keys.Enter);

            remEscOrBackPressed =
                gamePadState.Buttons.Back == ButtonState.Pressed ||
                keyboardState.IsKeyDown(Keys.Escape);
        }

        void SelectMenuItem()
        {
            if ((keyboardState.IsKeyDown(Keys.Down) || GamePadDown) && remDownPressed == false)
            {
                currentMenuItem = (currentMenuItem + 1) % 3;
                soundBank.PlayCue("PongBallHit");
            }

            else if ((keyboardState.IsKeyDown(Keys.Up) || GamePadUp) && remUpPressed == false)
            {
                currentMenuItem = (currentMenuItem + 2) % 3;
                soundBank.PlayCue("PongBallHit");
            }

            else if (IsGameMangementButtonsPressed() && remSpaceOrStartPressed == false && remEscOrBackPressed == false)
            {
                // Quit app.
                if (currentMenuItem == 2 ||
                    keyboardState.IsKeyDown(Keys.Escape) ||
                    gamePadState.Buttons.Back == ButtonState.Pressed)
                {
                    game.Exit();
                }
                else
                {
                    // Start game
                    gameMode = GameMode.Game;
                    GameMgr = new GameManager(this, soundBank);
                    GameMgr.StartNewGame();
                    // Set multiplayer if it was selected in menu.
                    // Otherwise the computer controls the left paddle
                    GameMgr.MultiPlayer = currentMenuItem == 1;
                } 
            }
        }

        bool IsGameMangementButtonsPressed()
        {
            return (keyboardState.IsKeyDown(Keys.Space) ||
                                keyboardState.IsKeyDown(Keys.LeftControl) ||
                                keyboardState.IsKeyDown(Keys.RightControl) ||
                                keyboardState.IsKeyDown(Keys.Enter) ||
                                gamePadState.Buttons.A == ButtonState.Pressed ||
                                gamePadState.Buttons.Start == ButtonState.Pressed ||
                // Back or Escape exits our game on Xbox 360 and Windows
                                keyboardState.IsKeyDown(Keys.Escape) ||
                                gamePadState.Buttons.Back == ButtonState.Pressed);
        }

        void GetCurrentPadAndKeyboardState()
        {
           gamePadState = Microsoft.Xna.Framework.Input.GamePad.GetState(PlayerIndex.One);
           gamePad2State = Microsoft.Xna.Framework.Input.GamePad.GetState(PlayerIndex.Two);
            keyboardState = Keyboard.GetState();

            GamePadUp = gamePadState.DPad.Up == ButtonState.Pressed ||
                gamePadState.ThumbSticks.Left.Y > 0.5f;
            GamePadDown = gamePadState.DPad.Down == ButtonState.Pressed ||
                gamePadState.ThumbSticks.Left.Y < -0.5f;
            GamePad2Up = gamePad2State.DPad.Up == ButtonState.Pressed ||
                gamePad2State.ThumbSticks.Left.Y > 0.5f;
            GamePad2Down = gamePad2State.DPad.Down == ButtonState.Pressed ||
                gamePad2State.ThumbSticks.Left.Y < -0.5f;
        }

    }
}
